package com.guli.product.service.impl;
import com.guli.product.entity.BrandEntity;
import com.guli.product.entity.CategoryEntity;
import com.guli.product.service.BrandService;
import com.guli.product.service.CategoryService;
import com.guli.product.vo.BrandVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.product.dao.CategoryBrandRelationDao;
import com.guli.product.entity.CategoryBrandRelationEntity;
import com.guli.product.service.CategoryBrandRelationService;
import org.springframework.util.CollectionUtils;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;

    @Override
    public List<CategoryBrandRelationEntity> queryPage(Map<String, Object> params) {
        Long brandId = Long.parseLong((String) params.get("brandId"));
        return baseMapper.selectList(new QueryWrapper<CategoryBrandRelationEntity>()
                .eq("brand_id", brandId));
    }

    @Override
    public List<BrandVo> brandList(Long catId) {
        //查询所有分类Id为catId的品牌关联信息
        List<CategoryBrandRelationEntity> brandRelations = this.baseMapper
                .selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        //获取所有与该分类关联的所有品牌Id
        List<Long> brandIds = brandRelations
                .stream().map(CategoryBrandRelationEntity::getBrandId).collect(Collectors.toList());
        List<BrandEntity> brands = null;
        if (!CollectionUtils.isEmpty(brandIds)) {
            //根据所有品牌Id查询所有品牌
            brands = brandService.list(new QueryWrapper<BrandEntity>().in("brand_id", brandIds));
        }
        //封装brandVo
        List<BrandVo> brandVos = null;
        if (!CollectionUtils.isEmpty(brands)) {
            brandVos = brands.stream().map(brand -> {
                BrandVo brandVo = new BrandVo();
                BeanUtils.copyProperties(brand, brandVo);
                brandVo.setBrandId(brand.getBrandId());
                brandVo.setBrandName(brand.getName());
                return brandVo;
            }).collect(Collectors.toList());
        }
        return brandVos;
    }

    @Override
    public void saveategoryBrandRelation(CategoryBrandRelationEntity categoryBrandRelation) {
        Long catelogId = categoryBrandRelation.getCatelogId();
        Long brandId = categoryBrandRelation.getBrandId();
        CategoryEntity category = categoryService.getById(catelogId);
        BrandEntity brand = brandService.getById(brandId);
        categoryBrandRelation.setBrandName(brand.getName());
        categoryBrandRelation.setCatelogName(category.getName());
        this.save(categoryBrandRelation);
    }
}